package com.clone.surpassli.clone_cloudreader.ui.gank;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;

import com.clone.surpassli.clone_cloudreader.R;
import com.clone.surpassli.clone_cloudreader.adapter.MyFragmentPageAdapter;
import com.clone.surpassli.clone_cloudreader.base.BaseFragment;
import com.clone.surpassli.clone_cloudreader.databinding.FragmentGankBinding;
import com.clone.surpassli.clone_cloudreader.ui.gank.child.AndroidFragment;
import com.clone.surpassli.clone_cloudreader.ui.gank.child.CustomFragment;
import com.clone.surpassli.clone_cloudreader.ui.gank.child.EveryDayFragment;
import com.clone.surpassli.clone_cloudreader.ui.gank.child.WelfareFragment;

import java.util.ArrayList;

/**
 * Created by SurpassLi on 2017/10/9.
 * GankFragment 干货推荐界面
 */

public class GankFragment extends BaseFragment<FragmentGankBinding> {

    private ArrayList<String> mTitleList = new ArrayList<>(4);
    private ArrayList<Fragment> mFragmentList = new ArrayList<>(4);

    @Override
    public int setContent() {
        return R.layout.fragment_gank;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        showLoading();
        initFragmentList();
        /*
         * 注意使用的是：getChildFragmentManager，
         * 这样setOffscreenPageLimit()就可以添加上，保留相邻3个实例，切换时不会卡
         * 但会内存溢出，在显示时加载数据
         */
        MyFragmentPageAdapter myFragmentPageAdapter = new MyFragmentPageAdapter(getChildFragmentManager(), mFragmentList, mTitleList);
        bindingView.vpGank.setAdapter(myFragmentPageAdapter);
        bindingView.vpGank.setOffscreenPageLimit(3);
        myFragmentPageAdapter.notifyDataSetChanged();
        bindingView.taGank.setTabMode(TabLayout.MODE_FIXED);
        bindingView.taGank.setupWithViewPager(bindingView.vpGank);
        showContentView();
    }

    private void initFragmentList() {
        mTitleList.add("每日推荐");
        mTitleList.add("福利");
        mTitleList.add("干货订制");
        mTitleList.add("大安卓");
        mFragmentList.add(new EveryDayFragment());
        mFragmentList.add(new WelfareFragment());
        mFragmentList.add(new CustomFragment());
        mFragmentList.add(AndroidFragment.getNewInstance("Android"));
    }
}
